﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cecbData;

namespace cecbClasses.Data_Adapters.Setting
{
    public class Func_Settings
    {
        public Func_Settings() {

            dataContext = new DSSEntities();

        }

        DSSEntities dataContext;

        public String getLastMailID() {

            var idKey = (from c in dataContext.Settings
                          where c.settingID == 3
                         select c).First();


            return  idKey.Value.ToString();
        }

        

        public String getLastMail()
        {

            var idKey = (from c in dataContext.Settings
                         where c.settingID == 1
                         select c).First();


            return idKey.Value.ToString();
        }


        public String getLastMailPass()
        {

            var idKey = (from c in dataContext.Settings
                         where c.settingID == 2
                         select c).First();


            return idKey.Value.ToString();
        }


        public bool updateSetting(int setID , String newVal )
        {
            bool isupdated = false;

            try
            { 
                Settings settobj = dataContext.Settings.First(i => i.settingID == setID);
                 
                settobj.Value = newVal;
                 
                dataContext.SaveChanges();
                isupdated = true;


            }
            catch (Exception)
            {
                isupdated = false;
            }

            return isupdated;

        }

        public String getSettingValues(int setID)
        {
            try
            {
                var idKey = (from c in dataContext.Settings
                             where c.settingID == setID
                         select c).First();

                if (idKey.Value.ToString().Length > 0)
                    return idKey.Value.ToString();
                else
                    return "Not save file path";
            }
            catch (Exception)
            {

                return "Not save file path";
            }
        
        } 

        public List<Projects> getAllActiveProjects()
        {
            IQueryable<Projects> query = from c in dataContext.Projects
                                         where c.proj_status== true
                                        select c;

            List<Projects> Memdetails = query.ToList();

            return Memdetails;
        }

        public Boolean uodateProjectActivateDetails(string projRef)
        {

            try
            {

                Projects projectObj = dataContext.Projects.First(i => i.proj_reference == projRef);
                projectObj.proj_status = false;
                dataContext.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }
        }
        public bool updateEmailAddress(String mail , String pass)
        {
            bool isupdated = false;

            try
            {

                Settings settobj = dataContext.Settings.First(i => i.settingID == 1);
 
                settobj.Value = mail;
                 
                dataContext.SaveChanges();
                Settings settobjp = dataContext.Settings.First(i => i.settingID == 2);

                settobjp.Value = pass;

                dataContext.SaveChanges();
                isupdated = true;


            }
            catch (Exception)
            {
                isupdated = false;
            }

            return isupdated;

        }
    }

}
